function niu(arr, curYear) {
    //下标代表年 值代表数量
    var newNiu = 0;
    for (var i = curYear; i > 0; i--) {
        if (i >= 20) {
            arr[20] = 0;
            i = 20;
        } else if (i >= 15) {
            arr[i] = arr[i - 1];
        } else if (i >= 4 && i < 15) {
            arr[i] = arr[i - 1];
            // arr[1] += arr[i-1];
            newNiu += arr[i - 1];
            // console.log(i-1, arr[i-1]);
        } else {
            arr[i] = arr[i - 1];
            arr[i - 1] = 0;
        }
    }
    // console.log("---  ", newNiu);
    arr[0] += newNiu;
}

function calNiuCount(startCount, n) {
    var arr = new Array(n + 1);
    // console.log(arr);
    // arr[0] = n;
    arr[0] = startCount;
    // niu(arr);

    for (var i = 1; i <= n; i++) {
        niu(arr, i);
    }
    var sum = 0;
    arr.forEach((e) => {
        sum += e;
    });
    console.log(arr);
    return sum;
}
console.log(calNiuCount(1, 1));
console.log(calNiuCount(1, 2));
console.log(calNiuCount(1, 3));
console.log(calNiuCount(1, 4));
console.log(calNiuCount(1, 5));
console.log(calNiuCount(1, 6));
console.log(calNiuCount(1, 7));
// console.log(8, calNiuCount(1, 8));
// console.log(9 ,calNiuCount(1, 9));
// console.log(10, calNiuCount(1, 10));
// console.log(18, calNiuCount(1, 18));
// console.log(19, calNiuCount(1, 19));
console.log(20, calNiuCount(1, 20));
// console.log(21,  calNiuCount(1, 21));

var cows_num = 1;

function countcows2(years) {
    for (var i = 1; i <= years; i++) {
        if (i >= 4 && i < 15) {
            cows_num++;
            countcows2(years - i);
        }
        if (i == 20) cows_num--;
    }
    return cows_num;
}
console.log(countcows2(1));
cows_num = 1;
console.log(countcows2(2));
cows_num = 1;
console.log(countcows2(3));
cows_num = 1;
console.log(countcows2(4));
cows_num = 1;
console.log(countcows2(5));
cows_num = 1;
console.log(countcows2(6));
cows_num = 1;
console.log(countcows2(7));
cows_num = 1;
console.log(countcows2(20));


//小青蛙跳台阶
function jump(i, n) {
    console.log("jump - >", i);
    if (i > n)
        return 0;
    else if (i == n - 1 || i == n) {
        return 1;
    }
    return jump(i + 1, n) + jump(i + 2, n);
}

console.log(jump(0, 10));

function f(n) {
    //经过分析，f(2)=2也是一个临界条件。
    if (n <= 2) {
        return n;
    }
    return f(n - 1) + f(n - 2);
}
console.log(f(10));

//反转单链表。例如链表为：1->2->3->4。反转后为 4->3->2->1
function Node(i, next) {
    this.val = i;
    this.next = next;
}

function reverseList(root, p) {
    if (root == null) 
        return p;

    var newRoot = reverseList(root.next, root);
    root.next = p;
    return newRoot;
}
// var reverseList = function(head) {
//     if (head == null || head.next == null) 
//         return head;

//     var newRoot = reverseList(head.next);
//     head.next.next = head;
//     head.next = null;
//     return newRoot;
// };
var node5 = new Node(5);
var nodes = new Node(1, new Node(2, new Node(3, new Node(4, node5))));
console.log(reverseList(nodes));


//链表相交
var getIntersectionNode = function(headA, headB) {
    if(headA == null || headB == null) return null;
        var  pA = headA, pB = headB;
        // 在这里第一轮体现在pA和pB第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pA或pB相交就返回交点, 不相交最后就是null==null
        while(pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
};